<?php
class Address{
    protected $main_table = 'data_user_address';
	protected $main_id = 'addr_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert address
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $rows ?  $this->db->getInsertId() : '';		
		return $insert_id;	
	}
	
	/**
	* 	update address
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete address
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	

	/**
	* 	get address info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id, $field = '*',$where='') {
	if (!intval($id))
	my_jump('輸入的參數有誤!');
	$sql = "SELECT " . $field . " FROM " . $this->main_table . " WHERE " . $this->main_id . "='" . intval($id) . "' ";
	if($where){
		$sql .= " AND ".$where;
	}
	$data = $this->db->getRow($sql);
	return $data;
    }
	
	
	/**
	* 	get address array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='addr_id',$limit='10',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}      

	public function get_user_list($uid){
		$sql = "SELECT * from ".$this->main_table." WHERE addr_uid='".$uid."' order by addr_default desc ";
		$data = $this->db->getAll($sql);
		return $data;
		
	}
	
	
	public function get_addr_address($uid){
		$sql = "SELECT * FROM " . $this->main_table . " WHERE  addr_uid='" . intval($uid) . "' AND addr_default = '1'";
		$row = $this->db->getRow($sql);
		return $row;
		
	}
	

}

?>